package com.sgb.kjwl.viewmodel.shopping;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.fragment.shop.ShopTdIntroFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopTdIntroModel extends BaseViewModel {
    private ShopTdIntroFragment fragment;
    private ShoppingCarRepertory repertory;

    public ShopTdIntroModel(ShopTdIntroFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getData(String shopId, String channel) {
        // teamInfo:工队简介
        // teamPublish:我的发布
        // teamOffer:工种报价
        // teamAchieve:施工业绩
        // teamMember:工队人员
        // teamEquipment:自有设备
        Map<String, Object> map = new HashMap<>();
        map.put("id", shopId);
        map.put("channel", channel);
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().queryTeamInfoByStore(map).enqueue(new Callback<BaseEntity<ShopTeamInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ShopTeamInfoEntity>> call, Response<BaseEntity<ShopTeamInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    fragment.showEmpty();
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null == response.body().getData()){
                        fragment.showEmpty();
                    }else {
                        fragment.updateViews(response.body().getData());
                    }
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ShopTeamInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.showEmpty();
            }
        });
    }
}
